package cn.iocoder.yudao.module.erp.controller.admin.business.order.orderquotation;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderquotation.vo.OrderQuotationPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderquotation.vo.OrderQuotationRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderquotation.vo.OrderQuotationSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.orderquotation.OrderQuotationDO;
import cn.iocoder.yudao.module.erp.service.business.order.orderquotation.OrderQuotationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 报价方式")
@RestController
@RequestMapping("/erp/order-quotation")
@Validated
public class OrderQuotationController {

    @Resource
    private OrderQuotationService orderQuotationService;

    @PostMapping("/create")
    @Operation(summary = "创建报价方式")
    @PreAuthorize("@ss.hasPermission('erp:order-quotation:create')")
    public CommonResult<Long> createOrderQuotation(@Valid @RequestBody OrderQuotationSaveReqVO createReqVO) {
        return success(orderQuotationService.createOrderQuotation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报价方式")
    @PreAuthorize("@ss.hasPermission('erp:order-quotation:update')")
    public CommonResult<Boolean> updateOrderQuotation(@Valid @RequestBody OrderQuotationSaveReqVO updateReqVO) {
        orderQuotationService.updateOrderQuotation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报价方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:order-quotation:delete')")
    public CommonResult<Boolean> deleteOrderQuotation(@RequestParam("id") Long id) {
        orderQuotationService.deleteOrderQuotation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报价方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:order-quotation:query')")
    public CommonResult<OrderQuotationRespVO> getOrderQuotation(@RequestParam("id") Long id) {
        OrderQuotationDO orderQuotation = orderQuotationService.getOrderQuotation(id);
        return success(BeanUtils.toBean(orderQuotation, OrderQuotationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报价方式分页")
    @PreAuthorize("@ss.hasPermission('erp:order-quotation:query')")
    public CommonResult<PageResult<OrderQuotationRespVO>> getOrderQuotationPage(@Valid OrderQuotationPageReqVO pageReqVO) {
        PageResult<OrderQuotationDO> pageResult = orderQuotationService.getOrderQuotationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderQuotationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报价方式 Excel")
    @PreAuthorize("@ss.hasPermission('erp:order-quotation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderQuotationExcel(@Valid OrderQuotationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderQuotationDO> list = orderQuotationService.getOrderQuotationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报价方式.xls", "数据", OrderQuotationRespVO.class,
                        BeanUtils.toBean(list, OrderQuotationRespVO.class));
    }

}